package com.gaohe.service.impl;

import com.gaohe.mapper.BookMapper;
import com.gaohe.pojo.Book;
import com.gaohe.projectException.BusinessException;
import com.gaohe.service.BookService;
import com.gaohe.utils.Code;
import com.gaohe.utils.Message;
import com.gaohe.utils.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper mapper;

    @Override
    public Result listBooks() {
        List<Book> books = mapper.listBooks();
        return new Result(
                Code.GET_SUCCESS,
                books,
                Message.SUCCESS
        );
    }

    @Override
    public Result savaBook(Book book) {
        int res = mapper.addBook(book);

        return new Result(
                res > 0 ? Code.SAVE_SUCCESS : Code.SAVE_ERROR,
                null,
                res > 0 ? Message.SUCCESS : Message.ERROR
        );
    }

    @Override
    public String deleteById(Integer id) {
        int res = mapper.deleteById(id);
        return res > 0 ? "success" : "error";
    }

    @Override
    public List<Book> getByConditions(Book book) {
//        System.out.println(1 / 0);
        return mapper.getByConditions(book);
    }

    @Override
    public String updateById(Book book) {
        String[] words = {"666", "上链接", "偷狗贼"};
        String type = book.getType();
        String name = book.getName();
        String description = book.getDescription();
        for (String word : words) {
            if (type.contains(word) || name.contains(word) || description.contains(word)) {
                throw new BusinessException(
                        Code.BUSINESS_EXCEPTION_CODE,
                        Message.ILLEGAL_WORDS);
            }
        }
        int res = mapper.updateById(book);
        return res > 0 ? "success" : "error";
    }


}
